package com.infore.statisticsAndAlarm.export.controller;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.infore.statisticsAndAlarm.export.service.IExportService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "导出管理接口", tags = { "导出管理接口" })
@RestController
@RequestMapping("/exports")
public class ExportController {

	@Autowired
	IExportService IExportService;
	
	@ApiOperation(value = "断面水质评价报表数据导出")
	@ResponseBody
	@RequestMapping(value = "/datasStageExcel", method = RequestMethod.POST)
	public void datasStageExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String file = request.getParameter("filename").toString().trim();
		String flag = request.getParameter("flag").toString();
		String filename = "";
		byte[] result = {};
		if (flag.equals("1")) {
			filename = file + ".xls";
		} else if (flag.equals("2")) {
		    filename = file + ".pdf";
		} else if (flag.equals("3")) {
		    filename = file + ".doc";
		}
		result = IExportService.exportExcelData(request,flag);
		try {
			// 取得文件名。
			filename = java.net.URLEncoder.encode(filename, "UTF-8");
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
			response.addHeader("Content-Length", "" + result.length);
			response.setContentType("application/vnd.ms-excel;charset=gb2312");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			toClient.write(result);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@ApiOperation(value = "水质范围评价报表数据导出")
	@ResponseBody
	@RequestMapping(value = "/waterQualityExcel", method = RequestMethod.POST)
	public void waterQuality(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String file = request.getParameter("filename").toString().trim();
		String flag = request.getParameter("flag").toString();
		String filename = "";
		byte[] result = {};
		if (flag.equals("1")) {
			filename = file + ".xls";
		} else if (flag.equals("2")) {
		    filename = file + ".pdf";
		} else if (flag.equals("3")) {
		    filename = file + ".doc";
		}
		result = IExportService.waterQuality(request,flag);
		try {
			// 取得文件名。
			filename = java.net.URLEncoder.encode(filename, "UTF-8");
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
			response.addHeader("Content-Length", "" + result.length);
			response.setContentType("application/vnd.ms-excel;charset=gb2312");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			toClient.write(result);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@ApiOperation(value = "自动站单站报表数据导出")
	@ResponseBody
	@RequestMapping(value = "/singleWaterQaulityReports", method = RequestMethod.POST)
	public void singleWaterQaulityReports(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String file = request.getParameter("filename").toString().trim();
		String flag = request.getParameter("flag").toString();
		String filename = "";
		byte[] result = {};
		if (flag.equals("1")) {
			filename = file + ".xls";
		} else if (flag.equals("2")) {
		    filename = file + ".pdf";
		} else if (flag.equals("3")) {
		    filename = file + ".doc";
		}
		result = IExportService.singleWaterQaulityReports(request,flag);
		try {
			// 取得文件名。
			filename = java.net.URLEncoder.encode(filename, "UTF-8");
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
			response.addHeader("Content-Length", "" + result.length);
			response.setContentType("application/vnd.ms-excel;charset=gb2312");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			toClient.write(result);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@ApiOperation(value = "自动站多站报表数据导出")
	@ResponseBody
	@RequestMapping(value = "/moreWaterQaulityReports", method = RequestMethod.POST)
	public void moreWaterQaulityReports(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String file = request.getParameter("filename").toString().trim();
		String flag = request.getParameter("flag").toString();
		String filename = "";
		byte[] result = {};
		if (flag.equals("1")) {
			filename = file + ".xls";
		} else if (flag.equals("2")) {
		    filename = file + ".pdf";
		} else if (flag.equals("3")) {
		    filename = file + ".doc";
		}
		result = IExportService.moreWaterQaulityReports(request,flag);
		try {
			// 取得文件名。
			filename = java.net.URLEncoder.encode(filename, "UTF-8");
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
			response.addHeader("Content-Length", "" + result.length);
			response.setContentType("application/vnd.ms-excel;charset=gb2312");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			toClient.write(result);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@ApiOperation(value = "地表水质公报数据导出")
	@ResponseBody
	@RequestMapping(value = "/waterQaulityPublicReports", method = RequestMethod.POST)
	public void waterQaulityPublicReports(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String file = request.getParameter("filename").toString().trim();
		String flag = request.getParameter("flag").toString();
		String filename = "";
		byte[] result = {};
		if (flag.equals("1")) {
			filename = file + ".xls";
		} else if (flag.equals("2")) {
		    filename = file + ".pdf";
		} else if (flag.equals("3")) {
		    filename = file + ".doc";
		}
		result = IExportService.waterQaulityPublicReports(request,flag);
		try {
			// 取得文件名。
			filename = java.net.URLEncoder.encode(filename, "UTF-8");
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
			response.addHeader("Content-Length", "" + result.length);
			response.setContentType("application/vnd.ms-excel;charset=gb2312");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			toClient.write(result);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
